﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DAO;
using DTO;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors.DXErrorProvider;
using DevExpress.XtraGrid.Views.BandedGrid;

namespace QLDaiLy
{
    public partial class XemDanhSachDaiLy :XtraUserControl
    {
        private int MaChucVu;
        public XemDanhSachDaiLy()
        {
            InitializeComponent();
            //BandedGridColumn col = gridViewDanhSachDaiLy.Columns.Add("Country") as BandedGridColumn;
            //col.Visible = true;
          //  gridViewDanhSachDaiLy.Columns["Delete"].Visible = false;
           
       
        }

        public XemDanhSachDaiLy(int MaChucVu)
        {
            // TODO: Complete member initialization
            InitializeComponent();
            this.MaChucVu = MaChucVu;
            if (MaChucVu == 1)
            {
                gridViewDanhSachDaiLy.Columns["ThuTien"].Visible = false;
                gridViewDanhSachDaiLy.Columns["PhieuXuat"].Visible = false;
                gridViewDanhSachDaiLy.Columns["XoaThat"].Visible = false;
                gridViewDanhSachDaiLy.Columns["CapNhat"].Visible = false;
                gridViewDanhSachDaiLy.Columns["XoaGia"].Visible = false;
            }
            if (MaChucVu == 2)
            {
                gridViewDanhSachDaiLy.Columns["ThuTien"].Visible = false;
                gridViewDanhSachDaiLy.Columns["PhieuXuat"].Visible = false;
                gridViewDanhSachDaiLy.Columns["CapNhat"].Visible = false;
                gridViewDanhSachDaiLy.Columns["XoaGia"].Visible = false;
            }
            if (MaChucVu == 3)
            {
                gridViewDanhSachDaiLy.Columns["ThuTien"].Visible = false;
                gridViewDanhSachDaiLy.Columns["PhieuXuat"].Visible = false;
                gridViewDanhSachDaiLy.Columns["XoaThat"].Visible = false;
            }
            if (MaChucVu == 4)
            {

                gridViewDanhSachDaiLy.Columns["ThuTien"].Visible = false;
                gridViewDanhSachDaiLy.Columns["CapNhat"].Visible = false;
                gridViewDanhSachDaiLy.Columns["XoaGia"].Visible = false;
                gridViewDanhSachDaiLy.Columns["XoaThat"].Visible = false;
            }
            if (MaChucVu == 5)
            {
                gridViewDanhSachDaiLy.Columns["PhieuXuat"].Visible = false;
                gridViewDanhSachDaiLy.Columns["CapNhat"].Visible = false;
                gridViewDanhSachDaiLy.Columns["XoaGia"].Visible = false;
                gridViewDanhSachDaiLy.Columns["XoaThat"].Visible = false;
            }
        }

        private void LoadDanhDanhSachDaiLy()
        {

            List<DaiLyDTO> list = new List<DaiLyDTO>();
            list = DaiLyDAO.DanhSachDaiLyDangHoatDong();
            gridDaiLyControl.DataSource = list;
        }
        private void XemDanhSachDaiLy_Load(object sender, EventArgs e)
        {
            LoadDanhDanhSachDaiLy();
        }
        private void gridViewDanhSachDaiLy_Click(object sender, EventArgs e)
        {
            
            
            
          }
        private void gridViewDanhSachDaiLy_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
        {
            if (e.IsGetData){
                e.Value = QLDaiLy.Properties.Resources.CapNhatIcon;
                }
              
                if (e.Column.Caption == "Delete")
                {
                    e.Value = QLDaiLy.Properties.Resources.deletexoagia;
                }
                if (e.Column.Caption == "Hoá Đơn")
                {
                    e.Value = QLDaiLy.Properties.Resources.HoaDon;
                }
                if (e.Column.Caption == "Phiếu Thu")
                {
                    e.Value = QLDaiLy.Properties.Resources.ThuTien;
                }
                if (e.Column.Caption == "Xoá Thật")
                {
                    e.Value = QLDaiLy.Properties.Resources.delete;
                }

        }
       
        private void repositoryItemPictureEdit1_Click(object sender, EventArgs e)
        {
            
            string MaDaiLy = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["MaDaiLy"]).ToString();
            string TenDaiLy = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["TenDaiLy"]).ToString();
            string TenLoaiDaiLy = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["TenLoaiDaiLy"]).ToString();
            string TenQuan = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["TenQuan"]).ToString();
            string DiaChi = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["DiaChi"]).ToString();
            string DienThoai = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["DienThoai"]).ToString();
            string Email = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["Email"]).ToString();
            string NgayTiepNhan = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["NgayTiepNhan"]).ToString();
            TableLayoutPanel tb = (TableLayoutPanel)this.Parent;
            tb.Controls.Remove(tb.GetControlFromPosition(0, 1));
           // CapNhatDaiLy frm = new CapNhatDaiLy(_dl);
            CapNhatDaiLy frm = new CapNhatDaiLy(MaDaiLy,TenDaiLy,TenLoaiDaiLy,TenQuan,DiaChi,DienThoai,Email,NgayTiepNhan);
            tb.Controls.Add(frm, 0, 1);
        }

        private void repositoryItemPictureDelete_Click(object sender, EventArgs e)
        {
             DialogResult dr = MessageBox.Show("Bạn có muốn xóa đại lý này không?", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
             if (dr == DialogResult.OK)
             {

                 string maDaiLy = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["MaDaiLy"]).ToString();
                 bool kq = DaiLyDAO.XoaGia(maDaiLy);
                 if (kq == true)
                 {
                     MessageBox.Show("Xoá thành công !");
                     // dataGridView.Rows.Add();
                     LoadDanhDanhSachDaiLy();
                 }
                 else
                 {
                     MessageBox.Show("Có lổi khi xoá đại lý");
                 }
             }
        }

        private void labelControl7_Click(object sender, EventArgs e)
        {

        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void repositoryItemPhieuThu_Click(object sender, EventArgs e)
        {
            string MaDaiLy = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["MaDaiLy"]).ToString();
            string TenDaiLy = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["TenDaiLy"]).ToString();
            string NoCuaDaiLy = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["NoCuaDaiLy"]).ToString();

            TableLayoutPanel tb = (TableLayoutPanel)this.Parent;
            tb.Controls.Remove(tb.GetControlFromPosition(0, 1));
            PhieuThuTien frm = new PhieuThuTien(MaDaiLy, TenDaiLy, NoCuaDaiLy);
            tb.Controls.Add(frm, 0, 1);
        }

        private void repositoryItemPictureHoaDon_Click(object sender, EventArgs e)
        {
            PhieuXuatDTO px = new PhieuXuatDTO();
            px.MaDaiLy = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["MaDaiLy"]).ToString();
            // px.TenDaiLy = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["TenDaiLy"]).ToString();
            TableLayoutPanel tb = (TableLayoutPanel)this.Parent;
            tb.Controls.Remove(tb.GetControlFromPosition(0, 1));
            PhieuXuat frm = new PhieuXuat(px);
            tb.Controls.Add(frm, 0, 1);
        }

        private void repositoryItemXoaThat_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Bạn có muốn xóa đại lý này không?", "Thoát chương trình", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.OK)
            {
                string MaDaiLy = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["MaDaiLy"]).ToString();
                if (DaiLyDAO.deleteDaiLy(MaDaiLy))
                {
                    // MessageBox.Show("Xoá thành công");
                    LoadDanhDanhSachDaiLy();
                }
                else
                {
                    MessageBox.Show("Xoá thành thất bại");
                }
            }
        }
     }
}
